#1. Subsetting the data
repub2 = subset(data, (party == "Republican" | party_lean == "Republican Party"))
dem2 = subset(data, (party == "Democrat" | party_lean == "Democratic Party"))

stargazer(data[, c("message_main", "c_donate_1", "collective_guilt", "social_prox", "worriedwarming", "president","us_help")], summary.stat = c("n", "mean", "sd", "min", "max"),
          covariate.labels = c("Aid Index", "Personal Donation" , "Collective Guilt", 
                               "Social Proximity", "GW Worry Index","Presidential Aid","US Gov Aid"))

#3. Outcomes focusing on specific variables
# Function to calculate statistics including standard deviation
outcome_collapse <- function(df, func){
  if(func == "mean"){
    means = df %>% summarise(message_main = mean(message_main, na.rm =T),
                             c_donate_1 = mean(c_donate_1, na.rm =T),
                             collective_guilt = mean(collective_guilt, na.rm =T),
                             social_prox = mean(social_prox, na.rm=T),
                             worriedwarming = mean(worriedwarming, na.rm=T),
                             president = mean(president, na.rm=T),
                             us_help = mean(us_help, na.rm=T))
    means = (t(as.data.frame(means)))
    colnames(means) = "Mean"
    return(means)
  }
  if(func == "sd"){
    sds = df %>% summarise(message_main = sd(message_main, na.rm =T),
                           c_donate_1 = sd(c_donate_1, na.rm =T),
                           collective_guilt = sd(collective_guilt, na.rm =T),
                           social_prox = sd(social_prox, na.rm=T),
                           worriedwarming = sd(worriedwarming, na.rm=T),
                           president = sd(president, na.rm=T),
                           us_help = sd(us_help, na.rm=T))
    sds = (t(as.data.frame(sds)))
    colnames(sds) = "SD"
    return(sds)
  }
}

# Combine means and standard deviations into one table
mean_sd_table = cbind(outcome_collapse(data, "mean"),
                      outcome_collapse(dem2, "mean"),
                      outcome_collapse(repub2, "mean"),
                      outcome_collapse(data, "sd"),
                      outcome_collapse(dem2, "sd"),
                      outcome_collapse(repub2, "sd"))

colnames(mean_sd_table) = c("Mean",
                            "Mean (Democrat)",
                            "Mean (Republican)",
                            "SD",
                            "SD (Democrat)",
                            "SD (Republican)")

print(xtable(mean_sd_table))

# Function to calculate min, max, and N
outcome_collapse <- function(df, func){
  if(func == "min"){
    mins = df %>% summarise(message_main = min(message_main, na.rm =T),
                            c_donate_1 = min(c_donate_1, na.rm =T),
                            collective_guilt = min(collective_guilt, na.rm=T),
                            social_prox = min(social_prox, na.rm=T),
                            worriedwarming = min(worriedwarming, na.rm=T),
                            president = min(president, na.rm=T),
                            us_help = min(us_help, na.rm=T))
    mins = (t(as.data.frame(mins)))
    colnames(mins) = "Minimum"
    return(mins)
  }
  if(func == "max"){
    maxs = df %>% summarise(message_main = max(message_main, na.rm =T),
                            c_donate_1 = max(c_donate_1, na.rm =T),
                            collective_guilt = max(collective_guilt, na.rm=T),
                            social_prox = max(social_prox, na.rm=T),
                            worriedwarming = max(worriedwarming, na.rm=T),
                            president = max(president, na.rm=T),
                            us_help = max(us_help, na.rm=T))
    maxs = (t(as.data.frame(maxs)))
    colnames(maxs) = "Maximum"
    return(maxs)
  }
  if(func == "n"){
    ns = df %>% summarise(message_main = sum(!is.na(message_main)),
                          c_donate_1 = sum(!is.na(c_donate_1)),
                          collective_guilt = sum(!is.na(collective_guilt)),
                          social_prox = sum(!is.na(social_prox)),
                          worriedwarming = sum(!is.na(worriedwarming)),
                          president = sum(!is.na(president)),
                          us_help = sum(!is.na(us_help)))
    ns = (t(as.data.frame(ns)))
    colnames(ns) = "N"
    return(ns)
  }
}

# Combine min, max, and N into one table
min_max_n_table = cbind(outcome_collapse(data, "min"),
                        outcome_collapse(data, "max"),
                        outcome_collapse(data, "n"),
                        outcome_collapse(dem2, "n"),
                        outcome_collapse(repub2, "n"))

colnames(min_max_n_table) = c("Minimum",
                              "Maximum",
                              "N",
                              "N (Democrat)",
                              "N (Republican)")

print(xtable(min_max_n_table))


